package com.fs.common.utils;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fs.common.pojo.Health;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 * 体质计算方法
 */
public class ConstitutionAlgorithmUtils {

    private static Logger logger = LoggerFactory.getLogger(ConstitutionAlgorithmUtils.class);

    public static String SymptomPattern = ".*[1-7]+[A-D]+";

    /**
     * 1、睡眠情况：A、入睡困难  B、早醒多梦  C、睡眠很浅
     * 2、大小便情况：A、大便干燥、排便困难 B、大便溏、不成形  C、产后排尿困难
     * 3、出汗情况：A、白天出汗多  B、睡觉出汗多  C、头部汗多
     * 4、头面情况：A、头发稀黄  B、最近常掉头发  B、头面部油腻
     * 5、手脚情况：A、手脚冰凉  B、手脚心热   C、手脚青筋多  D、四肢有水肿
     * 6、情志情况：A、脾气大、易发怒  B、经常情绪低落、生闷气
     * 7、其它情况：A、身上经常长疹子  B、平常容易感冒  C、经常扁桃体发炎  D、有口臭
     *
     * 判断模型
     *
     * 面部特征：
     * 1，颜色：面白 面黑 面红 面黄 面青 正常
     * 2，亮度：少光泽 有光泽
     * 舌部特征：
     * 1，舌裂痕：无裂纹 有裂纹
     * 2，舌头胖瘦：胖瘦正常 舌胖
     * 3，舌苔薄厚：苔薄  苔厚
     * 4，舌苔颜色：苔白 苔黄
     * 5，舌质颜色：舌暗红 舌淡白 舌淡红 舌红 舌深红
     *
     * 1.平和：舌淡红*10+苔薄*10+有光泽*10+正常*10+无任何症状*60
     * 2.气虚：舌胖*20+有裂纹*20+2B*20+7B*20+3A*20+1B*10
     * 3.阴虚：舌红*20+有裂纹*10+3B*30+5B*10+1A*20+7C*10+2A*10
     * 4.阳虚：舌胖*20+苔白*20+5A*30+2B*20+7B*10
     * 5.气郁：少光泽*10+舌暗红*20+1A*30+2A*10+6B*30
     * 6.血瘀：舌暗红*30+少光泽*10+5C*30+7C*10+6A*10+4A*10
     * 7.痰湿：舌胖*20+苔白*10+3C*20+4C*30+2B*20
     * 8.湿热：舌胖*20+苔黄*20+舌红*10+6A*20+7A*30
     * 9.血虚：舌淡白*20+4A*30+1B*20+4B*30
     * 10.脾虚：舌胖*10+面黄*20+有裂纹*20+5D*10+2B*10+7B*20+4A*20
     * 11.积食：苔厚*40+2A*10+5B*10+7D*40
     */

    /**
     * data=[面白#少光泽#有裂纹#胖瘦正常#苔薄#苔黄#舌淡白#1A#2B#3A#4A#5B#6A#7B#8A#9B#10A#11A#]
     * @param data
     */
    public static JSONObject getMedicalResult(String data) {
        //平和：舌淡红*10+苔薄*10+有光泽*10+正常*10+无任何症状*60
        String pinghe = "舌淡红,苔薄,有光泽,正常,有光泽";
        //气虚：舌胖*20+有裂纹*20+2B*20+7B*20+3A*20+1B*10
        String qixu = "舌胖,有裂纹,2B,7B,3A,1B";
        //阴虚：舌红*20+有裂纹*10+3B*30+5B*10+1A*20+7C*10+2A*10
        String yinxu = "舌红,有裂纹,3B,5B,1A,7C,2A";
        //阳虚：舌胖*20+苔白*20+5A*30+2B*20+7B*10
        String yangxu = "舌胖,苔白,5A,2B,7B";
        //气郁：少光泽*10+舌暗红*20+1A*30+2A*10+6B*30
        String qiyu = "少光泽,舌暗红,1A,2A,6B";
        //血瘀：舌暗红*30+少光泽*10+5C*30+7C*10+6A*10+4A*10
        String xueyu ="舌暗红,少光泽,5C,7C,6A,4A";
        //痰湿：舌胖*20+苔白*10+3C*20+4C*30+2B*20
        String tanshi = "舌胖,苔白,3C,4C,2B";
        //湿热：舌胖*20+苔黄*20+舌红*10+6A*20+7A*30
        String shire = "舌胖,苔黄,舌红,6A,7A";
        //血虚：舌淡白*20+4A*30+1B*20+4B*30
        String xuexu = "舌淡白,4A,1B,4B";
        //脾虚：舌胖*10+面黄*20+有裂纹*20+5D*10+2B*10+7B*20+4A*20
        String pixu="舌胖,面黄,有裂纹,5D,2B,7B,4A";
        //积食：苔厚*40+2A*10+5B*10+7D*40
        String jishi ="苔厚,2A,5B,7D";

        //计算每个体质的分数
        double pinghePoint = 0,
               qixuPoint = 0,
               yinxuPoint = 0,
               yangxuPoint = 0,
               qiyuPoint = 0,
               xueyuPoint = 0,
               tanshiPoint = 0,
               shirePoint = 0,
               xuexuPoint = 0,
               pixuPoint = 0,
               jishiPoint = 0,
               resultPoint = 0;
        String[] param = data.split("#");
        StringBuffer poinContent = new StringBuffer();

        for (String p:param){
            if(pinghe.contains(p)){//平和：舌淡红*10+苔薄*10+有光泽*10+正常*10+无任何症状*60
                if (FaceAndTongueDesc.TONGUE_SHE_DAN_HONG.equals(p)){
                    pinghePoint += 10;
                }
                if (FaceAndTongueDesc.TONGUE_TAI_BAO.equals(p)){
                    pinghePoint += 10;
                }
                if (FaceAndTongueDesc.FACE_LIGHT_YOU_GUANG_ZE.equals(p)){
                    pinghePoint += 10;
                }
                if (FaceAndTongueDesc.FACE_COLOR_NORMAL.equals(p)){
                    pinghePoint += 10;
                }
            }
            if(qixu.contains(p)){//气虚：舌胖*20+有裂纹*20+2B*20+7B*20+3A*20+1B*10
                if (FaceAndTongueDesc.TONGUE_SHE_PANG.equals(p)){
                    qixuPoint += 20;
                }
                if (FaceAndTongueDesc.TONGUE_YOU_LIE_WEN.equals(p)){
                    qixuPoint += 20;
                }
                if ("2B".equals(p)){
                    qixuPoint += 20;
                }
                if ("7B".equals(p)){
                    qixuPoint += 20;
                }
                if ("3A".equals(p)){
                    qixuPoint += 20;
                }
                if ("1B".equals(p)){
                    qixuPoint += 10;
                }
            }
            if (yinxu.contains(p)){//阴虚：舌红*20+有裂纹*10+3B*30+5B*10+1A*20+7C*10+2A*10
                if (FaceAndTongueDesc.TONGUE_SHE_HONG.equals(p)){
                    yinxuPoint += 20;
                }
                if (FaceAndTongueDesc.TONGUE_YOU_LIE_WEN.equals(p)){
                    yinxuPoint += 10;
                }
                if ("3B".equals(p)){
                    yinxuPoint += 30;
                }
                if ("5B".equals(p)){
                    yinxuPoint += 10;
                }
                if ("1A".equals(p)){
                    yinxuPoint += 20;
                }
                if ("7C".equals(p)){
                    yinxuPoint += 10;
                }
                if ("2A".equals(p)){
                    yinxuPoint += 10;
                }
            }
            if (yangxu.contains(p)){//阳虚：舌胖*20+苔白*20+5A*30+2B*20+7B*10
                if (FaceAndTongueDesc.TONGUE_SHE_PANG.equals(p)){
                    yangxuPoint += 20;
                }
                if (FaceAndTongueDesc.TONGUE_TAI_BAI.equals(p)){
                    yangxuPoint += 20;
                }
                if ("5A".equals(p)){
                    yangxuPoint += 30;
                }
                if ("2B".equals(p)){
                    yangxuPoint += 20;
                }
                if ("7B".equals(p)){
                    yangxuPoint += 10;
                }
            }
            if (qiyu.contains(p)){//气郁：少光泽*10+舌暗红*20+1A*30+2A*10+6B*30
                if (FaceAndTongueDesc.FACE_LIGHT_SHAO_GUANG_ZE.equals(p)){
                    qiyuPoint += 10;
                }
                if (FaceAndTongueDesc.TONGUE_SHE_AN_HONG.equals(p)){
                    qiyuPoint += 20;
                }
                if ("1A".equals(p)){
                    qiyuPoint += 30;
                }
                if ("2A".equals(p)){
                    qiyuPoint += 10;
                }
                if ("6B".equals(p)){
                    qiyuPoint += 30;
                }
            }
            if (xueyu.contains(p)){//血瘀：舌暗红*30+少光泽*10+5C*30+7C*10+6A*10+4A*10
                if (FaceAndTongueDesc.TONGUE_SHE_AN_HONG.equals(p)){
                    xueyuPoint += 30;
                }
                if (FaceAndTongueDesc.FACE_LIGHT_SHAO_GUANG_ZE.equals(p)){
                    xueyuPoint += 10;
                }
                if ("5C".equals(p)){
                    xueyuPoint += 30;
                }
                if ("7C".equals(p)){
                    xueyuPoint += 10;
                }
                if ("6A".equals(p)){
                    xueyuPoint += 10;
                }
                if ("4A".equals(p)){
                    xueyuPoint += 10;
                }
            }
            if (tanshi.contains(p)) {//痰湿：舌胖*20+苔白*10+3C*20+4C*30+2B*20
                if (FaceAndTongueDesc.TONGUE_SHE_PANG.equals(p)){
                    tanshiPoint += 20;
                }
                if (FaceAndTongueDesc.TONGUE_TAI_BAI.equals(p)){
                    tanshiPoint += 10;
                }
                if ("3C".equals(p)){
                    tanshiPoint += 20;
                }
                if ("4C".equals(p)){
                    tanshiPoint += 30;
                }
                if ("2B".equals(p)){
                    tanshiPoint += 20;
                }
            }
            if (shire.contains(p)) {//湿热：舌胖*20+苔黄*20+舌红*10+6A*20+7A*30
                if (FaceAndTongueDesc.TONGUE_SHE_PANG.equals(p)){
                    shirePoint += 20;
                }
                if (FaceAndTongueDesc.TONGUE_TAI_HUANG.equals(p)){
                    shirePoint += 20;
                }
                if (FaceAndTongueDesc.TONGUE_SHE_HONG.equals(p)){
                    shirePoint += 10;
                }
                if ("6A".equals(p)){
                    shirePoint += 20;
                }
                if ("7A".equals(p)){
                    shirePoint += 30;
                }
            }
            if (xuexu.contains(p)) {//血虚：舌淡白*20+4A*30+1B*20+4B*30
                if (FaceAndTongueDesc.TONGUE_SHE_DAN_BAI.equals(p)){
                    xuexuPoint += 20;
                }
                if ("4A".equals(p)){
                    xuexuPoint += 30;
                }
                if ("1B".equals(p)){
                    xuexuPoint += 20;
                }
                if ("4B".equals(p)){
                    xuexuPoint += 30;
                }
            }

            if(pixu.contains(p)){//脾虚：舌胖*10+面黄*20+有裂纹*20+5D*10+2B*10+7B*20+4A*20
                if (FaceAndTongueDesc.TONGUE_SHE_PANG.equals(p)){
                    pixuPoint += 10;
                }
                if (FaceAndTongueDesc.FACE_COLOR_MIAN_HUANG.equals(p)){
                    pixuPoint += 20;
                }
                if (FaceAndTongueDesc.TONGUE_YOU_LIE_WEN.equals(p)){
                    pixuPoint += 20;
                }
                if ("5D".equals(p)){
                    pixuPoint += 10;
                }
                if ("2B".equals(p)){
                    pixuPoint += 10;
                }
                if ("7B".equals(p)){
                    pixuPoint += 20;
                }
                if ("4A".equals(p)){
                    pixuPoint += 20;
                }
            }

            if (jishi.contains(p)){//积食：苔厚*40+2A*10+5B*10+7D*40
                if (FaceAndTongueDesc.TONGUE_TAI_HOU.equals(p)){
                    jishiPoint += 40;
                }
                if ("2A".equals(p)){
                    jishiPoint += 10;
                }
                if ("5B".equals(p)){
                    jishiPoint += 10;
                }
                if ("7D".equals(p)){
                    jishiPoint += 40;
                }
            }

        }
        //无任何症状
        if (noSymptom(data)){
            pinghePoint += 60;
        }

        //每项体质得分大于等于50分记录,低于50分归0
        int standardPoint = 49;
        StringBuffer resultContent = new StringBuffer();
        poinContent.append("平和=").append(formatPoint(pinghePoint)).append("#");
        if (pinghePoint > standardPoint){
            resultContent.append("、").append("平和");
        }else{
            pinghePoint = 0;
        }
        poinContent.append("气虚=").append(formatPoint(qixuPoint)).append("#");
        if (qixuPoint > standardPoint){
            resultContent.append("、").append("气虚");
        }else{
            qixuPoint = 0;
        }
        poinContent.append("阴虚=").append(formatPoint(yinxuPoint)).append("#");
        if (yinxuPoint > standardPoint){
            resultContent.append("、").append("阴虚");
        }else{
            yinxuPoint = 0;
        }
        poinContent.append("阳虚=").append(formatPoint(yangxuPoint)).append("#");
        if (yangxuPoint > standardPoint){
            resultContent.append("、").append("阳虚");
        }else{
            yangxuPoint = 0;
        }
        poinContent.append("气郁=").append(formatPoint(qiyuPoint)).append("#");
        if (qiyuPoint > standardPoint){
            resultContent.append("、").append("气郁");
        }else{
            qiyuPoint = 0;
        }
        poinContent.append("血瘀=").append(formatPoint(xueyuPoint)).append("#");
        if (xueyuPoint > standardPoint){
            resultContent.append("、").append("血瘀");
        }else{
            xueyuPoint = 0;
        }
        poinContent.append("痰湿=").append(formatPoint(tanshiPoint)).append("#");
        if (tanshiPoint > standardPoint){
            resultContent.append("、").append("痰湿");
        }else{
            tanshiPoint = 0;
        }
        poinContent.append("湿热=").append(formatPoint(shirePoint)).append("#");
        if (shirePoint > standardPoint){
            resultContent.append("、").append("湿热");
        }else{
            shirePoint = 0;
        }
        poinContent.append("血虚=").append(formatPoint(xuexuPoint)).append("#");
        if (xuexuPoint > standardPoint){
            resultContent.append("、").append("血虚");
        }else{
            xuexuPoint = 0;
        }
        poinContent.append("脾虚=").append(formatPoint(pixuPoint)).append("#");
        if (pixuPoint > standardPoint){
            resultContent.append("、").append("脾虚");
        }else{
            pixuPoint = 0;
        }
        poinContent.append("积食=").append(formatPoint(jishiPoint));
        if (jishiPoint > standardPoint){
            resultContent.append("、").append("积食");
        }else{
            jishiPoint = 0;
        }

        if (resultContent.length()>0){
            resultContent.deleteCharAt(0);
        }

        resultPoint = 100 - qixuPoint*0.1 - yinxuPoint*0.1 - yangxuPoint*0.1 - qiyuPoint*0.1 - xueyuPoint*0.1
                - tanshiPoint*0.1 - shirePoint*0.1 - xuexuPoint*0.1 - pixuPoint*0.1 - jishiPoint*0.1
                + pinghePoint*0.2;
        if (resultPoint > 100){ resultPoint = 100;}

        JSONObject object = new JSONObject();
        object.put("point", formatPoint(resultPoint));//总分
        object.put("resultContent", resultContent);//内容
        object.put("pointContent", poinContent);//分数
        if (logger.isInfoEnabled()){
            logger.info(object.toJSONString());
        }
        return object;
    }

    private static String formatPoint(double point){
        String pointFmt = "%.0f";
        return String.format(pointFmt, point);
    }

    private static boolean noSymptom(String data){
        return !data.matches(SymptomPattern);
    }

    public static void main(String[] args) {
        getMedicalResult("正常#少光泽#有裂纹#胖瘦正常#苔薄#苔白#舌暗红#1B#2B#3C#4B#5B#6B#7D");
    }
}
